package com.favccxx.message.domain.message.adapter.rabbitmq;

import com.favccxx.message.domain.message.MessageProtocol;
import com.favccxx.message.domain.message.adapter.CamelAdapter;
import com.favccxx.message.domain.message.entity.endpoint.MessageEndPoint;
import com.favccxx.message.domain.message.entity.endpoint.RabbitmqEndPoint;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.component.rabbitmq.RabbitMQComponent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitmqAdapter implements CamelAdapter {


    @Override
    public String getUri(CamelContext camelContext, MessageEndPoint endPoint) {
        if (endPoint instanceof RabbitmqEndPoint) {
            RabbitmqEndPoint rabbitmqEndPoint = (RabbitmqEndPoint) endPoint;

            RabbitMQComponent rabbitMQComponent = new RabbitMQComponent();
            rabbitMQComponent.setConnectionFactory(getConnectionFactory(rabbitmqEndPoint));
            rabbitMQComponent.setDurable(rabbitmqEndPoint.isDuribility());
            rabbitMQComponent.setAutoDelete(rabbitmqEndPoint.isAutoDelete());

            camelContext.addComponent(endPoint.getCode(), rabbitMQComponent);

            StringBuffer rabbitBf = new StringBuffer();
            rabbitBf.append(endPoint.getCode()).append(":");

            if (StringUtils.isNotBlank(rabbitmqEndPoint.getExchangeName())) {
                rabbitBf.append(rabbitmqEndPoint.getExchangeName()).append("?");
            }
            if (StringUtils.isNoneBlank(rabbitmqEndPoint.getRoutingName())) {
                rabbitBf.append("routingKey=").append(rabbitmqEndPoint.getRoutingName());
            }
            if (StringUtils.isNoneBlank(rabbitmqEndPoint.getQueueName())) {
                rabbitBf.append("&queue=").append(rabbitmqEndPoint.getQueueName());
            }
            if (StringUtils.isNoneBlank(rabbitmqEndPoint.getMessageType())) {
                rabbitBf.append("&exchangeType=").append(rabbitmqEndPoint.getMessageType().toLowerCase());
            } else {
                rabbitBf.append("&exchangeType=topic");
            }

            rabbitBf.append("&autoDelete=").append(rabbitmqEndPoint.isAutoDelete());

            String uri = rabbitBf.toString();
            if (log.isDebugEnabled()) {
                log.debug("***** Rabbit Adapter uri -> " + uri);
            }

            return uri;
        }

        log.error("!!!!! RabbitMQ adapter could not generate uri, because the message endpoint is not instance of RabbitmqEndPoint.");
        return null;
    }

    @Override
    public String getDriverType() {
        return MessageProtocol.RABBITMQ.getName();
    }


    /**
     * 获取RabbitMQ 连接信息
     *
     * @param rabbitmqEndPoint
     * @return
     */
    private ConnectionFactory getConnectionFactory(RabbitmqEndPoint rabbitmqEndPoint) {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(rabbitmqEndPoint.getUrl());
        factory.setPort(rabbitmqEndPoint.getPort());
        factory.setUsername(rabbitmqEndPoint.getUsername());
        factory.setPassword(rabbitmqEndPoint.getPassword());
        factory.setVirtualHost(rabbitmqEndPoint.getVirtualHost());

        return factory;
    }


}
